﻿@{
    ViewBag.Title = "组管理";
}


<div style="width: 100%;">
    <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100%;">
                    <a id="one" class="mini-button" iconcls="icon-add" plain="true" onclick="addRow()">增加</a>
                    <a id="two" class="mini-button" iconcls="icon-remove" plain="true" onclick="removeRow()">删除</a>
                    <a id="three" class="mini-button" iconcls="icon-save" plain="true" onclick="saveData()">保存</a>
                    <a id="four" class="mini-button" iconcls="icon-add" plain="true" onclick="addUser()">用户</a>
                    <a id="five" class="mini-button" iconcls="icon-add" plain="true" onclick="addCompany()">公司</a>
                    <a id="six" class="mini-button" iconcls="icon-add" plain="true" onclick="addMenu()">菜单</a>
                </td>
                <td style="white-space: nowrap;">
                    <input id="key" class="mini-textbox" emptytext="请输入关键字" style="width: 250px;" onenter="onKeyEnter" />
                    <a class="mini-button" plain="true" onclick="search()">查询</a>
                </td>
            </tr>
        </table>
    </div>
</div>

<div class="mini-fit" style="height: 100%;">

    <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;"
        url="GetGroup" idfield="id" pagesize="20"
        allowcelledit="true" allowcellselect="true" multiselect="true"
        editnextonenterkey="true" editnextrowcell="true">
        <div property="columns">
            <div type="indexcolumn"></div>
            <div type="checkcolumn"></div>
            <div field="GID" visible="false"></div>
            <div field="GName" name="GName" headeralign="center" width="200px" vtype="required">
                名称
                <input property="editor" class="mini-textbox" style="width: 100%;" minwidth="200px" />
            </div>
            <div type="checkboxcolumn" field="IsFunction" width="80px">
                功能组
            </div>
            <div type="checkboxcolumn" field="IsValid" width="60px" align="center" headeralign="center">
                有效性
            </div>
            <div field="GNote" width="120px" headeralign="center">
                备注
                <input property="editor" class="mini-textarea" style="width: 200px;" minheight="80" />
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">

    mini.parse();

    var grid = mini.get("datagrid1");
    grid.load({
        fail: function () {
        }
    });

    function search() {
        var key = mini.get("key").getValue();

        grid.load({
            key: key, fail: function () {
            }
        });
    }

    function onKeyEnter(e) {
        search();
    }

    function addRow() {
        var newRow = { name: "New Row" };
        grid.addRow(newRow, 0);
        grid.beginEditCell(newRow, "GName");
    }

    function removeRow() {
        var rows = grid.getSelecteds();
        if (rows.length > 0) {
            grid.removeRows(rows, true);
        }
    }

    function saveData() {

        grid.validate();
        if (grid.isValid() == false) {
            var error = grid.getCellErrors()[0];
            grid.beginEditCell(error.record, error.column);
            return;
        }

        var added = grid.getChanges("added");
        var jsonAdd = mini.encode(added);

        var modified = grid.getChanges("modified");
        var jsonEdit = mini.encode(modified);

        var removed = grid.getChanges("removed");
        var jsonDel = mini.encode(removed);

        grid.loading("保存中，请稍后......");
        $.ajax({
            url: "Save",
            data: { add: jsonAdd, edit: jsonEdit, del: jsonDel },
            type: "post",
            success: function (text, flag, res) {
                grid.unmask();
                if (res.responseText == "") {
                    grid.reload();
                }
                else
                    top.mini.alert(res.responseText);
            },
            error: function (text, flag, res) {
                top.mini.alert(res.responseText);
            }
        });
    }


    function addMenu() {
        var row = grid.getSelected();
        if (!row) {
            top.mini.showTips({ content: "请选择行！！！", state: "info", x: "center", y: "center", timeout: 1000 });
            return;
        }
        mini.open({
            url: "/SystemData/Group/GroupMenu",
            title: "菜单权限",
            width: "800px",
            height: "420px",
            allowResize: true,
            allowDrag: true,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { gid: row.GID };
                iframe.contentWindow.SetData(data);
            }
        });
    }

    function addUser() {
        var row = grid.getSelected();
        if (!row) {
            $.tip({ text: "请选择一行", staytime: 1000 });
            return;
        }
        mini.open({
            url: "/SystemData/Group/GroupUser?gid=" + row.GID,
            title: "用户",
            width: "670px",
            height: "399px",
            allowResize: false,
            allowDrag: true,
            onload: function () {
                //var iframe = this.getIFrameEl();
                //var data = { MID: node.MID };
                //iframe.contentWindow.SetData(data);
            }
        });
    }

    function addCompany() {
        var row = grid.getSelected();
        if (!row) {
            $.tip({ text: "请选择一行", staytime: 1000 });
            return;
        }
        mini.open({
            url: "/SystemData/Group/GroupCompany?gid=" + row.GID,
            title: "公司",
            width: "550px",
            height: "390px",
            allowResize: false,
            allowDrag: true,
            onload: function () {
                //var iframe = this.getIFrameEl();
                //var data = { MID: node.MID };
                //iframe.contentWindow.SetData(data);
            }
        });
    }


</script>
